import os
import time
import json
import pandas as pd

from rqalpha_mod_stu_api.data_source import St_Database_KDataSource

default_bundle_path = os.path.abspath(os.path.expanduser('~/.rqalpha/bundle'))
rqdatac = St_Database_KDataSource(default_bundle_path)


def golden_cross_stock_tasks():
    #
    code_list = rqdatac.all_instruments('stock').order_book_id.tolist()
    batch_params = []
    for c in code_list:
      batch_params.append({"universe": c})
    #
    from nextt.tests.rqalpha.golden_cross_stock import __config__, __file__
    start = time.time()
    ret = group_file_task(__file__, batch_params, __config__)
    print(time.time() - start)
    print(json.dumps(ret, indent=2))



if __name__ == '__main__':

    from nextt.tasks import group_file_task

    # # 第一步生成买卖信号，按股票分布式处理
    # golden_cross_stock_tasks()
    

    # 第二步回测股池
    from nextt.tasks import year_split_file_tasks
    
    param_list = [
        {"max_posi_count": 100, "over_count_proc":1, "less_count_proc": 1},
        {"max_posi_count": 100, "over_count_proc":1, "less_count_proc": 2},
        {"max_posi_count": 100, "over_count_proc":2, "less_count_proc": 1},
        {"max_posi_count": 100, "over_count_proc":2, "less_count_proc": 2}
        ] 
    from nextt.group.group_stock_trading import __config__, __file__
    start = time.time()
    ret = year_split_file_tasks(__file__, param_list, __config__, 40)
    print(time.time() - start)
    print(json.dumps(ret, indent=2))